import escapeRegExp from '../helpers/escapeRegExp';

export default async function usernameSearchFilterArgQuery(
	query,
	value,
	{ args }
) {
	const {
		app: {
			model: { User },
		},
	} = this;
	const val = value.trim();
	if (args?.filter?.uid || !val) return;
	const users = await User.find(
		{ username: { $regex: escapeRegExp(val) } },
		{ _id: 1 }
	);
	query.uid = { $in: users.map(x => x._id) };
}
